// pages/order/order.js
//获取应用实例
var app = getApp();
var util = require('../../utils/util.js');
import WxValidate from '../../utils/validate';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: ['未完成订单', '所有订单'],
        columns: ["未婚", "已婚", "离异"],
        gender: 0,
        housing: ["高层", "洋房", "别墅", "楼梯房"],
        housingTypes: 0,
        attributes: ["现金分期", "光大二抵", "光大一抵", "海尔", "其他"],
        attributesTypes: 0,
        land: ["出让", "划拨", "经济运用房"],
        landTypes: 0,
        refund: ["等额本息", "先息后本", "等额本金", "等额等息"],
        refundTypes: 0,
        yesNo: [],
        accStatus: [],
        bank: [],
        array: [],
        formList: "",
        productId: "",
        dynamic: "",
        show: false,
        show1: false,
        url1: "",
        url2: "",
        url3: "",
        form: {
            basicBorrowerName: null,
            basicBorrowerCard: null,
            basicBorrowerPhone: null,
            pledgeHouseAddress: null,
            basicLoansMoney: null,
            productId: null
        },
        orderNumber: "",
        intentionRatioNum: "",
        basicLoansMoneyNum: "",
        intentionRatioNum1: 1,
        basicLoansMoneyNum1: 3,
        formShow: false,
        miaoshuImg: false,
        imgShow: true,
        imgShow1: true,
        region: [],
        addtellds: {
            addtellHidden: false, //弹出框显示/隐藏
        },
        customItem: [],
        detailed: '请选择',
        detailedTwo: "请选择",
        city: "",
        area: "",
        province: "",
        cityTwo: "",
        areaTwo: "",
        provinceTwo: "",
        addHouseShow: false,
        moveSpecialCode:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // util.isLoginModal();
        let thisObj = this;
        //获取消息列表
        this.getOrderList(this.data.page);
        thisObj.getIndexData();
        thisObj.setData({
            show: true,
            productId: wx.getStorageSync('productId'),
        })
    },
    onShow: function () {
        this.setData({
            addHouseShow: false
        })
    },
    onUnload: function () {
        wx.removeStorageSync("productId");
        wx.removeStorageSync("fapaiId");
    },
    // bindRegionChange: function (e) {
    //     this.setData({
    //         basicBorrowerAddress: e.detail.value[0] + ' ' + e.detail.value[1] + ' ' + e.detail.value[2]
    //     })
    // },
    //省市联动
    // bindRegionChangeOne: function (e) {
    //     var that = this
    //     //为了让选择框有个默认值，    
    //     that.setData({
    //         clas: ''
    //     })
    //     this.setData({
    //         //拼的字符串传后台
    //         detailed: e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2],
    //         //下拉框选中的值
    //         // region: e.detail.value,
    //         province: e.detail.code[0],
    //         city: e.detail.code[1],
    //         area: e.detail.code[2],
    //     })
    //     console.log('picker发送选择改变，携带值为', e)
    //     console.log(that.data.province, that.data.city, that.data.area)
    // },
    // bindRegionChangeTwo: function (e) {
    //     var that = this
    //     //为了让选择框有个默认值，    
    //     that.setData({
    //         clasTwo: ''
    //     })
    //     this.setData({
    //         //拼的字符串传后台
    //         detailedTwo: e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2],
    //         //下拉框选中的值
    //         // region: e.detail.value,
    //         provinceTwo: e.detail.code[0],
    //         cityTwo: e.detail.code[1],
    //         areaTwo: e.detail.code[2],
    //     })
    // },
    // bindRegionChange1: function (e) {
    //     this.setData({
    //         basicBorrowerMateAddress: e.detail.value[0] + ' ' + e.detail.value[1] + ' ' + e.detail.value[2]
    //     })
    // },
    // 分享
    onShareAppMessage: function (res) {
        var that = this;
        return {
            title: '贷投狗小程序',
            path: 'pages/order/order',
            success: function (res) {
                that.shareClick();
            },
            fail: function (res) {
            }
        }
    },
    thinkTank: function () {
        // 保存图片
        let that = this;
        wx.getSetting({
            success: function (res) {
                //不存在相册授权
                if (!res.authSetting['scope.writePhotosAlbum']) {
                    wx.openSetting({
                        success: () => {

                        }
                    })
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success: function () {
                            that.savePhoto();
                        },
                        fail: function (err) {
                            wx.showToast({
                                title: '授权失败',
                                icon: 'none'
                            })
                        }
                    })
                } else {
                    that.savePhoto();
                }
            }
        })

    },
    savePhoto: function () {
        util.https(app.globalData.api + "/applet/order/extensionProjectCode/" + this.data.productId, "GET", {},
            function (data) {
                if (data.code == 200) {
                    // 保存图片
                    wx.showActionSheet({
                        itemList: ['保存图片'],
                        itemColor: '#000000',
                        success: (result) => {
                            if (result.tapIndex == 0) {
                                wx.getImageInfo({
                                    src: data.data,
                                    success: (ret) => {
                                        var path = ret.path;
                                        wx.saveImageToPhotosAlbum({
                                            filePath: path,
                                            success: () => {
                                                wx.showToast({
                                                    title: '保存成功',
                                                })
                                            },
                                            fail: () => {
                                                wx.showToast({
                                                    title: '保存失败',
                                                })
                                            }
                                        })
                                    }
                                })
                            }
                        },
                    });
                }
            })

    },
    // 选择订单类型
    bindPickerChange: function (e) {
        this.setData({
            productId: e.currentTarget.dataset.id,
        })
        this.dynamicData(this.data.productId)
        wx.setStorageSync("productId", this.data.productId)
    },
    /**
    * 获取数据
    */
    getIndexData: function () {
        var that = this;
        let id = wx.getStorageSync('productId')
        let fapaiId = wx.getStorageSync('fapaiId')
        if (fapaiId) {
            util.https(app.globalData.api + "/applet/order/projectList?projectId=" + fapaiId, "GET", {},
                function (data) {
                    if (data.code == 200) {
                        that.setData({
                            array: data.data,
                        })
                        if (that.data.array.length > 0) {
                            if (id) {
                                that.setData({
                                    productId: id
                                })
                            } else {
                                that.setData({
                                    productId: data.data[0].id
                                })
                            }
                            that.dynamicData(that.data.productId)
                            wx.setStorageSync("productId", that.data.productId)
                        }
                    } else {
                        util.toolTip(that, data.message)
                    }
                }
            )
        } else {
            util.https(app.globalData.api + "/applet/order/projectList", "GET", {},
                function (data) {
                    if (data.code == 200) {
                        that.setData({
                            array: data.data,
                        })
                        if (that.data.array.length > 0) {
                            if (id) {
                                that.setData({
                                    productId: id
                                })
                            } else {
                                that.setData({
                                    productId: data.data[0].id
                                })
                            }
                            that.dynamicData(that.data.productId)
                            wx.setStorageSync("productId", that.data.productId)
                        }
                    } else {
                        util.toolTip(that, data.message)
                    }
                }
            )
        }
    },
    // 请求动态表单标识
    dynamicData: function (obj) {
        var that = this;
        util.https(app.globalData.api + "/applet/order/dynamicFrom/" + obj, "GET", {},
            function (data) {
                if (data.code == 200) {
                    that.setData({
                        dynamic: data.data.minProcedureCode,
                        moveSpecialCode: data.data.moveSpecialCode,
                    })
                } else {
                    util.toolTip(that, data.message)
                }
            }
        )
    },

    lookMe: function (e) {
        wx.switchTab({
            url: '/pages/partner/partner',
        })
    },
    // 提交
    formSubmit: function (e) {
        var thisObj = this;
        let formData = e.detail.value;
        formData.productId = thisObj.data.productId;
        let card = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
        if (thisObj.data.dynamic == 1) {
            if (formData.basicBorrowerCard && formData.basicBorrowerName && formData.basicBorrowerPhone) {
                if (!card.test(formData.basicBorrowerCard)) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '请填写正确的申请人身份证号',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) { }
                        }
                    })
                    return false
                }
                if (!/^1[3456789]\d{9}$/.test(formData.basicBorrowerPhone)) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '请填写正确的申请人联系电话',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) { }
                        }
                    })
                    return false
                }
                delete formData.basicLoansMoney;
                delete formData.pledgeHouseAddress
                this.submit(formData)
            } else {
                wx.showModal({
                    title: '温馨提示',
                    content: '请完成信息录入',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            }
        } else {
            if (formData.basicBorrowerCard && formData.basicBorrowerName && formData.basicBorrowerPhone && formData.basicLoansMoney && formData.pledgeHouseAddress) {
                if (!card.test(formData.basicBorrowerCard)) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '请填写正确的申请人身份证号',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) { }
                        }
                    })
                    return false
                }
                if (!/^1[3456789]\d{9}$/.test(formData.basicBorrowerPhone)) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '请填写正确的申请人联系电话',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) { }
                        }
                    })
                    return false
                }
                this.submit(formData)
            } else {
                console.log("成功")
                wx.showModal({
                    title: '温馨提示',
                    content: '请完成信息录入',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            }
        }
    },
    submit: function (formData) {
        let thisObj = this;
        util.https(app.globalData.api + "/applet/order/submit", "POST", formData,
            function (data) {
                console.log(data, '00')
                if (data.code == 200) {
                    thisObj.setData({
                        show: false,
                        show1: true,
                        orderNumber: data.data
                    })
                    wx.pageScrollTo({
                        scrollTop: 0
                    })
                } else if (data.code == 204) {
                    thisObj.setData({
                        message: data.message,
                        addtellds: {
                            addtellHidden: true, //弹出框显示/隐藏
                        },
                    })
                } else {
                    wx.showModal({
                        title: '温馨提示',
                        content: data.message,
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }
            }
        )
    },
    modalCancel: function () {
        //弹出框取消操作
        this.setData({
            addtellds: {
                addtellHidden: false,
            }
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (!this.data.hasData) {
            return;
        }
        //获取消息列表
        this.getOrderList(this.data.page);
    },
    getOrderList: function (page) {
    },
    // 查看订单
    look: function () {
        let orderNumber = this.data.orderNumber
        let loginId = wx.getStorageSync("loginId")
        wx.setStorageSync('orderNumber', orderNumber)
        wx.setStorageSync('belongId', loginId)
        wx.redirectTo({
            url: '/pages/wallet/recharge',
        })
    },
})